package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.collect.ArrayUtil;
import com.qire.common.collect.ListUtil;
import com.qire.common.concurrent.Scheduler;
import com.qire.common.concurrent.ThreadUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.manhua.dal.IComicDataDAL;
import com.qire.manhua.dal.dao.ComicDatabase;
import com.qire.manhua.dal.dao.SearchHistoryDao;
import com.qire.manhua.model.po.SearchHistoryRecord;
import com.qire.manhua.model.viewModel.impl.ComicSearchViewModel;
import com.qire.manhua.model.vo.SearchWordsVO;
import com.qire.manhua.model.vo.SearchResultVO;
import com.qire.manhua.model.vo.SearchResultVO.SearchResultComicVO;
import com.qire.manhua.model.vo.SearchResultVO.SearchRecommendComicVO;

import java.util.List;

public class ComicSearchService extends DataService {

    private final IComicDataDAL comicDataDAL = WarehouseFactory.create(this, IComicDataDAL.class);
    private final SearchHistoryDao searchHistoryDao = ComicDatabase.database().searchHistoryDao();

    private final ComicSearchViewModel viewModel;

    public ComicSearchService(ComicSearchViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /**
     * 加载热门所有词
     */
    public void loadHotSearchData() {
        comicDataDAL.fetchHotSearchWords();
    }
    @StrategyAnnotation(event = Url.book_hot_words)
    public void fetchHotSearchWordsApiCallback(List<SearchWordsVO> dataList, boolean isCache) {
        if(ListUtil.isEmpty(dataList)) {
            return;
        }

        viewModel.clearHotItemList();
        for(SearchWordsVO wordsVO : dataList) {
            viewModel.addHotItem(wordsVO.book_id, wordsVO.book_name);
        }
        viewModel.refreshHotAdapter();
    }

    /**
     * 加载历史搜索数据
     */
    public void loadHistorySearchData() {
        ThreadUtil.once().andThen(data -> {
            List<SearchHistoryRecord> historyRecordList = searchHistoryDao.queryAllByIdSort(false);
            viewModel.clearHistoryItemList();
            for(SearchHistoryRecord historyRecord : historyRecordList) {
                viewModel.addHistoryItem(historyRecord.getSearchWords());
            }
        }).schedulerBy(Scheduler.Main).andThen(data ->{
            viewModel.refreshHistoryAdapter();
        }).accept();
    }

    /**
     * 清理历史搜索数据
     */
    public void removeHistorySearchData() {
        ThreadUtil.once().andThen(data -> {
            searchHistoryDao.deleteAll();
        }).accept();
    }

    /**
     * 添加搜索词到数据库
     * @param searchWords
     */
    public void saveSearchWords(String searchWords) {
        ThreadUtil.<SearchHistoryRecord>once().map(new SearchHistoryRecord(searchWords))
                .andThen(wordsRecord -> searchHistoryDao.insertReplace(wordsRecord))
                .accept();
    }

    /**
     * 搜索漫画
     * @param searchWords 搜索词
     * @param pageNo 页号
     */
    public void searchComic(String searchWords, int pageNo) {

        ThreadUtil.<String>once().map(searchWords).andThen(words -> {
            searchHistoryDao.insertReplace(new SearchHistoryRecord(words));
        }).accept();

        viewModel.clearResultItemList();
        viewModel.clearRecommendItemList();
        comicDataDAL.fetchSearchResult(searchWords, pageNo);

    }
    @StrategyAnnotation(event = Url.book_search_list_get)
    public void fetchSearchResultApiCallback(SearchResultVO dataVO, boolean isCache) {
        if(ObjectUtil.isNull(dataVO)) {
            return;
        }

        if(ArrayUtil.notEmpty(dataVO.rand_list)) {
            for(SearchRecommendComicVO comicVO : dataVO.rand_list) {
                viewModel.addRecommendItem(comicVO.id, comicVO.book_unruly, comicVO.book_name, comicVO.tags);
            }
        }

        if(ArrayUtil.notEmpty(dataVO.list)) {
            for(SearchResultComicVO comicVO : dataVO.list) {
                viewModel.addResultItem(comicVO.id, comicVO.book_unruly, comicVO.book_name, comicVO.book_author, comicVO.popularity, comicVO.short_title);
            }
        }

        if(ArrayUtil.notEmpty(dataVO.like_list)) {
            viewModel.addResultSeparate();
            for(SearchResultComicVO comicVO : dataVO.like_list) {
                viewModel.addResultItem(comicVO.id, comicVO.book_unruly, comicVO.book_name, comicVO.book_author, comicVO.popularity, comicVO.short_title);
            }
        }

        viewModel.refreshSearchResultUI(dataVO.item_count);
    }

    /**
     * 通过搜索词猜测联想词
     * @param searchWords 搜索词
     */
    public void guessAssociativeWords(String searchWords) {
        // todo: 可以是用延时队列来解决频繁出发接口的问题优化一下性能
        comicDataDAL.guessAssociativeWords(searchWords);
    }
    @StrategyAnnotation(event = Url.book_search_book)
    public void guessAssociativeWordsApiCallback(List<SearchWordsVO> dataList, boolean isCache) {
        if(ListUtil.isEmpty(dataList)) {
            return;
        }

        viewModel.clearGuessItemList();
        for(SearchWordsVO wordsVO : dataList) {
            viewModel.addGuessItem(wordsVO.id, wordsVO.book_name);
        }
        viewModel.refreshGuessAdapter();
    }

}
